<?php
/**
 * Created by PhpStorm.
 * Date: 2022/11/24 11:03
 */

namespace App\Traits;
use App\Models\User;

trait Ownable
{
    /**
     * 增加数据所有权筛选
     *
     * @param $query
     * @param User $owner
     * @return mixed
     */
    public function scopeOfOwner($query, User $owner)
    {
        if ($owner->isSupper()) return $query;

        if (!empty($owner->org_code)) {
            return $query->where('org_code', 'like', "{$owner->org_code}%");
        }

        return $query->where('user_id', $owner->id);
    }
}
